package ms.db.mysql;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@EnableAutoConfiguration
public class DruidConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(DruidConfiguration.class);
    @Autowired
    DBProperties DBProperties;

    @Bean
    @ConfigurationProperties(prefix = "spring.data.mysql.custom")
    DBProperties getMysqlProperties() {
        return new DBProperties();
    }


    @Bean
    ServletRegistrationBean druidServlet() {
        LOGGER.info("init Druid Servlet Configuration ");
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        // IP白名单
        servletRegistrationBean.addInitParameter("allow", DBProperties.getAllow());
        // IP黑名单(共同存在时，deny优先于allow)
        servletRegistrationBean.addInitParameter("deny", DBProperties.getDeny());
        //控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername", DBProperties.getUm());
        servletRegistrationBean.addInitParameter("loginPassword", DBProperties.getPwd());
        //是否能够重置数据 禁用HTML页面上的“Reset All”功能
        servletRegistrationBean.addInitParameter("resetEnable", DBProperties.getResetEnable() + "");
        return servletRegistrationBean;
    }

    @Bean
    FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

    @Bean()
//    @Primary  //在同样的DataSource中，首先使用被标注的DataSource
    DataSource dataSourceFactory() throws Exception {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(DBProperties.getUrl());
        datasource.setUsername(DBProperties.getUsername());
        datasource.setPassword(DBProperties.getPassword());
        datasource.setDriverClassName(DBProperties.getDriverClass());

        //configuration
        datasource.setInitialSize(DBProperties.getMinSize());
        datasource.setMinIdle(DBProperties.getMinSize());
        datasource.setMaxActive(DBProperties.getMaxSize());
        datasource.setMaxWait(DBProperties.getTimeout());
//        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
//        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery("select 1");
        datasource.setTestWhileIdle(true);
        datasource.setTestOnBorrow(false);
        datasource.setTestOnReturn(false);
        datasource.setPoolPreparedStatements(true);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(DBProperties.getMaxStatements());
        return datasource;
    }

}
